//
//  DoctorViewController.m
//
//
//  Created by Cyan on 16/9/12.
//
//

#import "DoctorViewController.h"
#import "DoctorView.h"
#import "AboutCell.h"

@interface DoctorViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
/**
 *  医生信息
 */
@property (nonatomic, strong) DoctorView *doctorView;

/**
 *  标签
 */
@property (nonatomic, strong) UIScrollView *channel;
/**
 *  医生简介和就诊信息的collectionview
 */
@property (nonatomic, strong) UICollectionView *collectionView;
/**
 *  collectionview 的 flowlayout
 */
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
/**
 *  标签字体下方的线
 */
@property (nonatomic, weak) UIView *lowLine;
/**
 *  记录被选中的 button
 */
@property (nonatomic, assign) NSInteger selected;
/**
 *  建议 button
 */
@property (nonatomic, strong) UIButton *suggestion;
/**
 *  记录
 */
@property (nonatomic, assign) NSInteger de;
@end

static NSString *identifier = @"collection";
@implementation DoctorViewController

#pragma mark - 数据模型
- (void)setModel:(DoctorListModel *)model {
    
    _model = model;
    
    self.doctorView.model = model;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self setUpRightButtonItem];
    [self.collectionView registerClass:[AboutCell class] forCellWithReuseIdentifier:identifier];
}


#pragma mark - 设置收藏按钮
- (void)setUpRightButtonItem{
    
    UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [right setImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
    [right setImage:[UIImage imageNamed:@"shixin"] forState:UIControlStateSelected];
    [right addTarget:self action:@selector(selected:) forControlEvents:UIControlEventTouchDown];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    
}

- (void)selected:(UIButton *)button {
    
    button.selected = !button.isSelected;
    
}

#pragma mark - 设置控件约束
- (void)setupUI {
    
    self.title = @"医生基本信息";
    
    [self.view addSubview:self.doctorView];
    [self.doctorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.height.equalTo(@150);
    }];
    
    [self.view addSubview:self.channel];
    [self.channel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.top.equalTo(self.doctorView.mas_bottom);
        make.height.equalTo(@50);
    }];
    
    [self.view addSubview:self.suggestion];
    [self.suggestion mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.height.equalTo(@60);
    }];
    
    
    //    self.collectionView.collectionViewLayout = self.flowLayout;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.channel.mas_bottom);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.bottom.equalTo(_suggestion.mas_top);
    }];
    _flowLayout.itemSize = self.collectionView.frame.size;
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}


#pragma mark - collectionview 数据源方法
/**
 *  返回每组 cell 的数量
 *
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 2;
}
/**
 *  返回 cell, 设置数据, cell 的重用
 *
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AboutCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    return cell;
}




#pragma mark - 添加选项
- (void)addChannel{
    
    UIView *line = [[UIView alloc]init];
    [_channel addSubview:line];
    
    line.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * 0.5, 5, 2, 40);
    line.backgroundColor = [UIColor lightGrayColor];
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    button1.tag = 0;
    button1.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 0.25, line.center.y);
    [_channel addSubview:button1];
    [button1 setTitleColor:kThemeColor forState:UIControlStateSelected];
    [button1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button1 setTitle:@"就诊条件" forState: UIControlStateNormal];
    [button1 addTarget:self action:@selector(didSelected:) forControlEvents:UIControlEventTouchDown];
    
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 0.75, line.frame.origin.y, 80, 40)];
    button2.tag = 1;
    button2.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 0.75, line.center.y);
    [_channel addSubview:button2];
    [button2 setTitleColor:kThemeColor forState:UIControlStateSelected];
    [button2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button2 setTitle:@"医生简介" forState: UIControlStateNormal];
    [button2 addTarget:self action:@selector(didSelected:) forControlEvents:UIControlEventTouchDown];
    
    UIView *lowLine = [[UIView alloc] initWithFrame:CGRectMake(button1.frame.origin.x, button1.frame.origin.y + 40, 80, 5)];
    self.lowLine = lowLine;
    lowLine.center = CGPointMake(button1.center.x, _lowLine.center.y);
    lowLine.backgroundColor = kThemeColor;
    
    [_channel addSubview:lowLine];
    [self didSelected:button1];
}

#pragma mark - button 点击的方法
- (void)didSelected:(UIButton *)button {
    
    
    button.selected = !button.isSelected;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.lowLine.center = CGPointMake(button.center.x, _lowLine.center.y);
    }];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:button.tag inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    for (UIView *btn in self.channel.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            if ([(UIButton *)btn tag] == self.selected) {
                [(UIButton *)btn setSelected:NO];
            }
            
        }
    }
    
    self.selected = button.tag;
}


#pragma mark - 正在滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat nextScale = scrollView.contentOffset.x;
    self.de = nextScale / self.collectionView.frame.size.width;
}

#pragma mark - 滚动停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    for (UIView *btn in self.channel.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            if ([(UIButton *)btn tag] == self.de) {
                [self didSelected:(UIButton *)btn];
            }
            
        }
    }
}

#pragma mark - 懒加载

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
     
        NSLog(@"%@",NSStringFromCGRect([UIScreen mainScreen].bounds));// 375.667 / 414,736
        
        if ([UIScreen mainScreen].bounds.size.width == 414) {
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 264, 414, 343 + 736 - 667) collectionViewLayout:self.flowLayout];
            NSLog(@"plus");
        }else {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 264, 375, 343) collectionViewLayout:self.flowLayout];
            NSLog(@"4.7");
        }
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
    }
    return _collectionView;
}

- (UIButton *)suggestion {
    
    if (!_suggestion) {
        _suggestion = [[UIButton alloc] init];
        [_suggestion setTitle:@"咨询医生" forState:UIControlStateNormal];
        [_suggestion setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_suggestion setBackgroundImage:[UIImage imageNamed:@"link_button_01"] forState:UIControlStateNormal];
        [_suggestion setBackgroundImage:[UIImage imageNamed:@"link_button_02"] forState:UIControlStateHighlighted];
    }
    return _suggestion;
}
- (DoctorView *)doctorView {
    if (!_doctorView) {
        _doctorView = [[DoctorView alloc] init];
        
    }
    return _doctorView;
}

- (UIScrollView *)channel {
    
    if (!_channel) {
        _channel = [[UIScrollView alloc] init];
        [self addChannel];
    }
    return _channel;
}
@end
